<template>
  <div class="inside-page">
    <div class="choice-wrap">
      <div class="choice-wrap-container clearfix">
        <div
          class="wrap-left"
          :style="{
            background:
              'url(' + info.images + ') center center / cover no-repeat',
          }"
        ></div>
        <div class="wrap-right">
          <p class="wrap-name">
            <span class="name" :title="info.name">{{ info.name }}</span>
            <span class="corner" :class="[info.type === 1 && 'active']"
              >{{ info.type === 0 ? '线上' : '线下' }}专场</span
            >
          </p>
          <p class="pro">主办单位：{{ info.for_name }}</p>
          <div class="wrap">
            <p>
              <i class="iconfont icon-map"></i>
              <span>地址：{{ info.address }}</span>
            </p>
            <p class="money" v-permission="['company']">
              <i class="iconfont icon-money"></i>
              <span>报名费用: ¥{{ info.money }}</span>
            </p>
          </div>
          <div class="time">
            <p>
              <span
                >双选会时间：{{ info.start_time }} - {{ info.end_time }}</span
              >
              <span>报名截止：{{ info.push_end_time }}</span>
            </p>
          </div>
        </div>
        <div class="company-data">
          <div class="data">
            <p class="num">{{ info.attend_company }}</p>
            <p class="text">参会单位</p>
          </div>
          <div class="data">
            <p class="num">{{ info.operating_number }}</p>
            <p class="text">提供岗位</p>
          </div>
          <div class="data">
            <p class="num">{{ info.attend_person }}</p>
            <p class="text">点击量</p>
          </div>
          <div class="data" v-if="info.user_num > 0">
            <p class="num">{{ info.user_num }}</p>
            <p class="text">报名人数</p>
          </div>
          <!-- <div class="submit" v-if="$store.state.user.authUser.usertype === 2"> -->
          <div class="submit" v-permission="['company']">
            <button class="confirm" type="button" @click="submit">
              {{ info.statusText }}
            </button>
            <!-- <div class="disabled">已报名</div> -->
          </div>
          <div class="submit" v-permission="['personal']">
            <button class="confirm" type="button" @click="submit">
              {{ info.statusText }}
            </button>
            <!-- <div class="disabled">已报名</div> -->
          </div>

        </div>
        <el-alert
          title="报名双选会成功后可查看企业名单并与企业在线沟通"
          type="warning"
          center
          :closable="false"
          v-if="info.status != 2"
          show-icon>
        </el-alert>
      </div>
    </div>
    <div class="choice-detail content">
      <p class="detail-title">双选会简介</p>
      <div
        class="intro"
        :style="{
          background:
            'url(' + info.desc_image + ') center center / cover no-repeat',
        }"
      ></div>
      <div class="intro-text">
        <div
          v-if="info.desc"
          class="con"
          :class="[toggleActive && 'active']"
          v-html="info.desc"
        ></div>
        <p v-if="showToggle" class="toggle-btn" @click="toggleText">
          {{ toggleActive ? '收起' : '展开' }}
        </p>
      </div>

      <div v-if="info.status == 2">
        <p class="detail-title">招聘企业</p>
        <div class="pos-filter">
          <p>职位筛选：</p>
          <client-only>
            <el-cascader
              size="small"
              placeholder="选择企业地点"
              :options="options"
              v-model="locat"
              @change="toFilter">
            </el-cascader>
          </client-only>
          <client-only>
            <el-select
              v-model="industry"
              clearable
              @change="toFilter"
              placeholder="选择行业"
            >
              <el-option
                v-for="item in search.industry"
                :key="item.id"
                :label="item.tagname"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </client-only>
          <client-only>
            <el-select
              v-model="posttypeid"
              clearable
              @change="toFilter"
              placeholder="选择职能"
            >
              <el-option
                v-for="item in search.postType"
                :key="item.id"
                :label="item.tagname"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </client-only>
        </div>
        <div class="choice-list">
          <div
            class="choice-item clearfix"
            v-for="j in jobList"
            :key="j.id"
            @click="jumpTo(j)"
          >
            <div class="choice-pos">
              <div class="text">
                <span class="choice-name" :title="j.cname">{{ j.cname }}</span>
              </div>
              <div class="wrap">
                <p>
                  <i class="iconfont icon-company"></i><span>{{ j.attrid }}</span>
                </p>
                <p>
                  <i class="iconfont icon-people"></i><span>{{ j.sizeid }}</span>
                </p>
              </div>
            </div>
            <div class="choice-type">
              <p class="name">{{ j.industryid }}</p>
              <div class="text">
                <span
                  class="tag"
                  v-for="(v, i) in j.welfare"
                  :key="'welfare_' + i"
                  >{{ v }}</span
                >
              </div>
            </div>
            <!---
            <div class="choice-num" v-if="j.place > 0">
              <p class="num">{{ j.place }}</p>
              <p class="text">序号</p>
            </div>
            <div class="choice-num" v-else>
              <p class="none">-</p>
            </div>
            --->

            <div class="choice-job">
              <p class="pos">
                <span>{{ j.place_num }}个工作地点</span>
                <span>{{ j.job_num }}个职位</span>
              </p>
              <p class="time">
                <i class="iconfont icon-time"></i>
                <span>申请截止：{{ j.end_time }}</span>
              </p>
            </div>
            <el-button type="primary" plain v-permission="['personal']" @click="connect(j.id)" class="choice-num">联系企业</el-button>
            <div
              class="choice-logo"
              :style="{
                background: 'url(' + j.logo + ') center center / cover no-repeat',
              }"
            ></div>
          </div>
        </div>
      </div>
      <client-only>
        <el-pagination
          hide-on-single-page
          background
          prev-text="上一页"
          next-text="下一页"
          layout="prev, pager, next"
          :current-page="pageNo"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </client-only>
    </div>
    <client-only>
      <el-dialog
        width="445px"
        :visible.sync="showConfirm"
        :show-close="false"
        custom-class="dialog-confirm"
      >
        <Confirm :info-title="infoTitle" @confirm="confirm" @cancel="cancel" />
      </el-dialog>
    </client-only>
  </div>
</template>

<script>
import { provinceAndCityDataPlus, CodeToText, TextToCode} from 'element-china-area-data'
import { getChoiceDetail } from '@/api/choice';
import { orderComfirm } from '@/api/order';
import {addChat1} from '@/api/job';
function stopBubbling(e) {
  e = window.event || e;
  if (e.stopPropagation) {
    e.stopPropagation();
  } else {
    e.cancelBubble = true;
  }
}
export default {
  asyncData: ({ req }) => ({
  }),
  components: {},
  data () {
    return {
      options: provinceAndCityDataPlus,
      locat: [],
      id: '',
      subState: 1,
      info: {
        desc: ''
      },
      showToggle: false,
      intro: '',
      toggleActive: false,
      jobList: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      search: {
        city: [],
        industry: [],
        postType: [],
      },
      cityid: '',
      industry: '',
      posttypeid: '',
      showConfirm: false,
      infoTitle: '确认加入该双选会吗？'
    }
  },
  head () {
    return {
      titleTemplate: (titleChunk) => {
        return `${titleChunk} - 大学生找工作、找实习`;
      }
    }
  },
  created () {
    this.id = this.$route.query.id;
    this.fetchData();
  },
  mounted () {
  },
  watch: {

  },
  computed: {
    checkNum () {
      return (num) => {
        const calcNum = this.pageNo * (num + 1);
        return calcNum < 10 ? '0' + calcNum : calcNum;
      }
    }
  },
  methods: {
    checkIntro () { // 检查公司简介
      if (this.info.desc) {
        (this.info.desc.indexOf('<p>', 1) > 3 || this.info.desc.length > 152) && (this.showToggle = true);
      }
    },
    toggleText () {
      this.toggleActive = !this.toggleActive;
    },
    handleCurrentChange (val) {
      this.pageNo = val;
      this.fetchData();
    },
    jumpTo (obj) {
      const newsUrl = this.$router.resolve({
        path: '/company/detail',
        query: { id: obj.cid }
      });
      window.open(newsUrl.href);
    },
    toFilter () {
      this.pageNo = 1;
      this.fetchData();
    },
    async fetchData () {
      await getChoiceDetail({
        id: this.id,
        province: this.locat[0],
        city: this.locat[1],
        industry: this.industry,
        posttypeid: this.posttypeid,
        page: this.pageNo,
      }).then(res => {
        if (res.code === 200) {
          this.total = res.data.count;
          this.info = res.data.info;
          if (this.info.status !== 2) {
            this.toggleActive = true;
          }
          this.jobList = res.data.list;
          this.search = res.data.search;
          this.checkIntro();
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      })
    },
    connect(id) {
      stopBubbling(event);
      addChat1({
        cid: id
      }).then(res => {
        if (res.code === 200) {
          this.$message.success('添加联系人成功，可进入交流平台进行交流');
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      })
    },
    submit () {
      //  0待支付，1已支付(待审核)，2已支付(审核通过)， 3已支付(审核不通过), 4取消订单，5退款，6已完成
      const state = this.info.status;
      switch (state) {
        case '':
          this.showConfirm = true;
          break;
        case 0:
          this.$router.push('/order');
          break;
        case 1:
          this.$message.info('待审核！');
          this.$router.push('/order');
          break;
        case 2:
          // this.$message.success('审核已通过！');
          // this.$router.push('/order');
          break;
        case 3:
          this.$$message.error('审核未通过！');
          break;
        case 6:
          this.$$message.success('已完成！');
          break;
        default:
          this.$message.info(this.info.statusText);
          break;
      }
    },
    cancel () {
      this.showConfirm = false;
    },
    confirm () {
      this.showConfirm = false;
      orderComfirm({
        id: this.id,
      }).then(res => {
        if (res.code === 200) {
          if (res.data.order_id === -1) {
            location.reload();
          }
          this.$message.success(res.msg || '提交成功！');
          this.$router.push({
            path: '/order/create',
            query: { id: res.data.order_id }
          })
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.inside-page {
  width: 100%;
  position: relative;
  .choice-wrap {
    width: 100%;
    min-height: 220px;
    padding-top: 40px;
    padding-bottom: 40px;
    background: #f6f7f8;
    position: relative;
    .choice-wrap-container {
      width: $base-width;
      margin: 0 auto;
      position: relative;
      .wrap-left {
        float: left;
        height: 140px;
        width: 140px;
        overflow: hidden;
        background: #fff;
        border-radius: 8px;
        text-align: center;
        img {
          height: 140px;
        }
      }
      .wrap-right {
        float: left;
        margin-left: 20px;
        position: relative;
        width: 750px;
        .wrap-name {
          line-height: 44px;
          .name {
            font-size: 28px;
            color: #24292f;
          }
          .corner {
            vertical-align: top;
            display: inline-block;
            margin-left: 5px;
            margin-top: 5px;
            width: 70px;
            height: 24px;
            line-height: 22px;
            font-size: 12px;
            text-align: center;
            border-radius: 4px;
            background: rgba(71, 113, 220, 0.3);
            color: $base-color;
            border: 1px solid $base-color;
            &.active {
              background: rgba(252, 112, 62, 0.3);
              color: $base-color-important;
              border: 1px solid $base-color-important;
            }
          }
        }
        .pro {
          margin: 10px 0;
          font-size: 14px;
          color: #999;
        }
        .wrap {
          p {
            margin-right: 30px;
            display: inline-block;
            line-height: 20px;
            vertical-align: middle;
            i {
              color: $base-color;
              font-size: 16px;
              vertical-align: middle;
            }
            span {
              vertical-align: middle;
              font-size: 14px;
              color: #666;
            }
          }
          .money {
            i,
            span {
              color: $base-color-danger;
              font-size: 14px;
              line-height: 20px;
            }
            i {
              font-size: 16px;
            }
          }
        }
        .time {
          padding-left: 15px;
          margin-top: 10px;
          position: relative;
          height: 20px;
          line-height: 20px;
          i,
          span {
            margin-right: 10px;
            font-size: 14px;
            color: #666;
          }
          &::before {
            content: '';
            height: 14px;
            width: 2px;
            background: $base-color;
            position: absolute;
            left: 2px;
            top: 3px;
          }
        }
      }
      .company-data {
        position: absolute;
        right: 0;
        top: 35%;
        transform: translateY(-20%);
        .data {
          float: left;
          text-align: center;
          padding-left: 15px;
          &:first-child {
            padding-left: 0;
          }
          .num {
            font-size: 28px;
            color: $base-color;
          }
          .text {
            font-size: 14px;
            color: #999;
          }
        }
        .submit {
          margin-top: 80px;
          button {
            width: 190px;
          }
          .disabled {
            display: inline-block;
            height: 45px;
            line-height: 43px;
            width: 150px;
            margin: 0 20px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 16px;
            background: #ccc;
            color: #f1f1f1;
            cursor: not-allowed;
          }
        }
      }
    }
  }

  .choice-detail {
    position: relative;
    .intro {
      height: 280px;
      background: #ebebeb;
    }
    .intro-text {
      position: relative;
      padding: 20px 0;
      p {
        font-size: 16px;
        line-height: 32px;
      }
      .con {
        font-size: 16px;
        line-height: 22px;
        color: #666;
        white-space: pre-wrap;
        height: 44px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        &.active {
          height: auto;
          overflow: visible;
          text-overflow: clip;
          word-break: break-all;
          word-wrap: break-word;
          white-space: normal;
          -webkit-line-clamp: inherit;
        }
      }
      .toggle-btn {
        position: absolute;
        right: 0;
        bottom: -5px;
        color: $base-color;
        font-size: 14px;
        &:hover {
          cursor: pointer;
          color: $base-color-hover;
        }
      }
    }
    .pos-filter {
      height: 50px;
      line-height: 40px;
      padding-top: 10px;
      p {
        float: left;
      }
      ::v-deep .el-input__inner {
        height: 30px;
        line-height: 30px;
      }
    }
  }

  .choice-list {
    margin: 20px auto;
    .choice-item {
      height: 120px;
      width: 100%;
      border: 1px solid #ebebeb;
      background: #fff;
      margin-bottom: 10px;
      padding-left: 40px;
      cursor: pointer;
      &:hover {
        background: #f6f7f8;
      }
      & > div {
        float: left;
      }
      .choice-pos {
        margin-top: 30px;
        line-height: 30px;
        margin-right: 20px;
        min-width: 300px;
        .choice-name {
          color: #242924;
          font-size: 18px;
          display: inline-block;
          max-width: 300px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .wrap {
          p {
            margin-right: 10px;
            color: #999;
            line-height: 20px;
            font-size: 14px;
            display: inline-block;
          }
          i {
            vertical-align: middle;
            color: #999;
            margin-right: 5px;
          }
          span {
            vertical-align: middle;
            color: #999;
          }
        }
      }
      .choice-type {
        margin-top: 30px;
        width: 210px;
        line-height: 30px;
        overflow: hidden;
        .name {
          font-size: 14px;
          color: #999;
        }
        .text {
          margin-top: 10px;
          white-space: nowrap;
          overflow: hidden;
          .tag {
            display: inline-block;
            min-width: 66px;
            height: 24px;
            line-height: 22px;
            font-size: 12px;
            color: #ccc;
            text-align: center;
            border: 1px solid #ebebeb;
            border-radius: 4px;
            margin-right: 6px;
          }
        }
      }
      .choice-num {
        margin-top: 30px;
        text-align: center;
        .num {
          color: $base-color;
          font-size: 28px;
        }
        .none {
          color: #999;
          font-size: 28px;
        }
        .text {
          font-size: 12px;
          color: #999;
        }
      }
      .choice-job {
        margin-top: 30px;
        width: 300px;
        line-height: 30px;
        .pos {
          span {
            margin-right: 20px;
            color: #666;
            font-size: 18px;
          }
        }
        .time {
          margin-top: 10px;
          i,
          span {
            color: #999;
            font-size: 14px;
          }
        }
      }
      .choice-logo {
        margin-top: 24px;
        margin-right: 24px;
        width: 74px;
        height: 74px;
        overflow: hidden;
        float: right;
      }
    }
  }
}
</style>
